<template>
	<div>
		<div class="poster" :style="'background: ' + hdInfo.color + ' url(' + hdInfo.img + ') center top / 100%  no-repeat'">
			<div class="btn" @click="hdyy"> <image :src="hdInfo.btn" mode="widthFix"></image> </div>
		</div>
		<u-toast ref="uToast"></u-toast>
	</div>
</template>

<script>
import { mapActions } from 'vuex';
const db = wx.cloud.database();
import share from '../../common/share/index.vue';
export default {
	data() {
		return {
			hdInfo: {
				color: "#e40000",
				img: "http://www.baixuemianshi.com/zhuanti/images/poster_cjyy.jpg",
				btn: "http://www.baixuemianshi.com/zhuanti/images/btn_cjyy.png"
			},
			name: ""
		};
	},

	onShow() {},
	onPullDownRefresh() {
		console.log('refresh');
		setTimeout(function() {
			uni.stopPullDownRefresh();
		}, 1000);
	},
	onPageScroll(e) {
		let that = this;
		that.scrollTop = e.scrollTop;
	},
	onLoad(options) {
		if (!options) {
		} else {
			this.name = options.name;
		}
	},
	computed: {
		userphone() {
			return this.$store.getters.userphone ? this.$store.getters.userphone : '';
		},
		useropenid() {
			return this.$store.getters.useropenid ? this.$store.getters.useropenid : '';
		},
		username() {
			return this.$store.getters.username ? this.$store.getters.username : '';
		},
		loginpage() {
			return this.$store.getters.loginpage ? this.$store.getters.loginpage : '';
		},
		favzw() {
			return this.$store.getters.favzw ? this.$store.getters.favzw : '';
		}
	},
	mounted() {
		this.name = this.name ? this.name : this.username;
		this.LogSetName(this.name);
		if (this.userphone) {
			if(!this.loginpage.includes('sk-cjyy')){
				this.subInfo()
			}
		}
	},
	watch: {
		userphone(val) {
			if (this.userphone) {
				if(!this.loginpage.includes('sk-cjyy')){
					this.subInfo()
				}
			}
		}
	},
	created() {
		let that = this;
		var openid = wx.getStorageSync('useropenid');
		console.log(openid);
		if (openid) {
			that.openid = openid;
		} else {
			that.getopenid();
		}
	},
	methods: {
		...mapActions(['LogSetSearchtimes', 'LogSetPhone', 'LogSetOpenid', 'LogSetName', 'LogSetPage', 'LogSetFavzw']),
		getopenid() {
			let that = this;
			wx.cloud.callFunction({
				name: 'getopenid',
				data: {},
				complete: res => {
					console.log('callFunction result: ', res);
					that.saveOpenid(res.result.openid);
				}
			});
		},
		saveOpenid(openid) {
			db.collection('user_info')
				.where({
					openid: openid
				})
				.get({
					success: function(res) {
						console.log('success: ', res);
						if (res.data.length == 0) {
							db.collection('user_info').add({
								data: {
									openid: openid,
									time: new Date()
								},
								success: function(res) {
									console.log('openid储存成功' + openid);
									wx.setStorageSync('useropenid', openid);
								}
							});
						} else {
							console.log('openid已储存' + openid);
							wx.setStorageSync('useropenid', openid);
						}
					}
				});
		},
		async getUserInfo() {
			const udInfo = await this.$getUser.getInfo(this.name);

		},
		toLogin() {
			let path = '../../my/login?navTlt=' + '省考成绩查询预约';
			uni.navigateTo({
				url: path
			});
		},
		subInfo(){
			let that = this
			if(!that.userphone){
				that.showToast("error","请先登录")
				setTimeout(() => {
					that.toLogin()
				},1000)
				return
			}
			
			if(that.loginpage.includes('sk-cjyy')){
				wx.showToast({
					icon: 'success',
					title: '订阅提醒成功',
					success() {
						that.changePath("../msxs/index")
					}
				});
				wx.hideLoading()
			}else{
				wx.cloud.callFunction({
					name: 'singleRegister',
					data: {
						fucname: 'hd_sk_cjyy',
						phone: that.userphone,
						user: that.name,
						remark: '省考成绩查询预约'
					},
					complete: res => {
						let zlStr = that.loginpage + ',sk-cjyy'
						that.LogSetPage(zlStr);
						wx.showToast({
							icon: 'success',
							title: '订阅提醒成功',
							success() {
								that.changePath("../msxs/index")
							}
						});
					}
				})
			}
		},
		hdyy() {
			let that = this
			const templateId = 'op6J4NqjQEELT2C8Tyagbaviwgk97I7g4EA9nY-t9Wk';
			console.log(templateId)
			if(that.loginpage.includes('sk-cjyy')){
				that.showToast("success","您已预约")
				wx.hideLoading()
				setTimeout(() => {
					that.changePath("../msxs/index")
				},1000)
				return
			}
			wx.requestSubscribeMessage({
				tmplIds: [templateId],
				success(res) {
					console.log(res);
					if (res[templateId] === 'accept') {
						db.collection('user_subscribe').add({
							data: {
								status: 1, //发送状态 0表示已发送，1表示未发送
								createTime: new Date(),
								templateId: templateId // 模板ID
							},
							success(res) {
								console.log('成功存入数据库！');
							},
							fail(res) {
								console.log('存入数据库失败！');
							}
						});
						
						that.subInfo()
					}else{
						wx.showToast({
							icon: 'none',
							title: '订阅提醒失败',
						})
						console.log(res[templateId], '失败')
					}
				},
				fail(req){
					console.log(req)
				}
			});
		},
		changePath(path){
			uni.navigateTo({
				url: path
			})
		},
		showToast(type, info) {
			this.$refs.uToast.show({
				type: type,
				icon: false,
				title: '失败主题',
				message: info,
			})
		},
	},
	components: { share }
};
</script>

<style lang="scss">
.poster {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100vh;
		z-index: 9999;
		background-size: 100%;
	}

	.poster .btn {
		position: absolute;
		bottom: 150rpx;
		left: 0;
		width: 100%;
	}
	.poster .btn image{display: block; width: 300rpx; margin: 0 auto; animation: pulse 1s linear infinite;}
	@keyframes pulse {
		0% {
		    -webkit-transform: scale3d(1, 1, 1);
		    transform: scale3d(1, 1, 1)
		  }
		  50% {
		    -webkit-transform: scale3d(1.05, 1.05, 1.05);
		    transform: scale3d(1.05, 1.05, 1.05)
		  }
		  100% {
		    -webkit-transform: scale3d(1, 1, 1);
		    transform: scale3d(1, 1, 1)
		  }
	}
	.name{position: fixed;bottom: 0; right: 0;  font-size: 18rpx; z-index: 10000;}
</style>
